package com.sky.controller.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.UserService;
import com.sky.vo.DishVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController("userdishcontroller")
@RequestMapping("user/dish")
@Api(tags="获取菜品的相关接口")
@Slf4j
public class DishController {
	@Autowired
	private RedisTemplate redisTemplate;
	@Autowired
	private UserService userservice;
	@GetMapping("/list")
	@ApiOperation("菜品的接口")
	public Result<List<DishVO>> dishList(Long categoryId)
	{
		log.info("获取该分类菜品的参数:{}",categoryId);
		//构造redis的数据源中的key，规则dish_分类id
		String key = "dish" + categoryId;
		//查询redis中是否存在菜品数量
		List<DishVO> dishvolist = (List<DishVO>) redisTemplate.opsForValue().get(key);
		//如果存在，直接返回，无须查询数据库
		if(dishvolist!=null&&dishvolist.size()>0)
		{
			return Result.success(dishvolist);
		}
		//如果不存在，查询数据库，将查询的数据放入redis中
		List<DishVO> dishlist = userservice.dishList(categoryId);
		redisTemplate.opsForValue().set(key, dishlist);
		return Result.success(dishlist);
	}
}
